<template>
  <el-dialog
    :title="!updForm.rentalId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="updForm" :rules="dataRule" ref="updForm" @keyup.enter.native="updFormSubmit()" label-width="80px">
    <el-form-item label="出租房名称" prop="rentalName">
      <el-input v-model="updForm.rentalName" placeholder="出租房名称"></el-input>
    </el-form-item>
    <el-form-item label="出租房标题" prop="rentalTitle">
      <el-input v-model="updForm.rentalTitle" placeholder="出租房标题"></el-input>
    </el-form-item>
    <el-form-item label="出租房面积" prop="rentalArea">
      <el-input v-model="updForm.rentalArea" placeholder="出租房面积"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="rentalPrice">
      <el-input v-model="updForm.rentalPrice" placeholder="租金"></el-input>
    </el-form-item>
    <el-form-item label="0-未出租 1-出租中 2-合租未满" prop="rentalStatus">
      <el-input v-model="updForm.rentalStatus" placeholder="0-未出租 1-出租中 2-合租未满"></el-input>
    </el-form-item>
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="updForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="updForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(县id)" prop="xianId">
      <el-input v-model="updForm.xianId" placeholder="地区id(县id)"></el-input>
    </el-form-item>
    <el-form-item label="出租房地址" prop="rentalAddress">
      <el-input v-model="updForm.rentalAddress" placeholder="出租房地址"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="updForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="dimension">
      <el-input v-model="updForm.dimension" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="出租房朝向" prop="rentalDirection">
      <el-input v-model="updForm.rentalDirection" placeholder="出租房朝向"></el-input>
    </el-form-item>
    <el-form-item label="小区id" prop="cellId">
      <el-input v-model="updForm.cellId" placeholder="小区id"></el-input>
    </el-form-item>
    <el-form-item label="小区名称" prop="cellName">
      <el-input v-model="updForm.cellName" placeholder="小区名称"></el-input>
    </el-form-item>
    <el-form-item label="户型" prop="unitName">
      <el-input v-model="updForm.unitName" placeholder="户型"></el-input>
    </el-form-item>
    <el-form-item label="出租房图集" prop="rentalImage">
      <el-input v-model="updForm.rentalImage" placeholder="出租房图集"></el-input>
    </el-form-item>
    <el-form-item label="装修情况（1 毛坯 2简装 3精装 4豪装）" prop="decolate">
      <el-input v-model="updForm.decolate" placeholder="装修情况（1 毛坯 2简装 3精装 4豪装）"></el-input>
    </el-form-item>
    <el-form-item label="出租房楼层" prop="rentalFloor">
      <el-input v-model="updForm.rentalFloor" placeholder="出租房楼层"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="updForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="updFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        updForm: {
          rentalId: 0,
          rentalName: '',
          rentalTitle: '',
          rentalArea: '',
          rentalPrice: '',
          rentalStatus: '',
          shenId: '',
          shiId: '',
          xianId: '',
          rentalAddress: '',
          longitude: '',
          dimension: '',
          rentalDirection: '',
          cellId: '',
          cellName: '',
          unitName: '',
          rentalImage: '',
          decolate: '',
          rentalFloor: '',
          brokerId: ''
        },
        dataRule: {
          rentalName: [
            { required: true, message: '出租房名称不能为空', trigger: 'blur' }
          ],
          rentalTitle: [
            { required: true, message: '出租房标题不能为空', trigger: 'blur' }
          ],
          rentalArea: [
            { required: true, message: '出租房面积不能为空', trigger: 'blur' }
          ],
          rentalPrice: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          rentalStatus: [
            { required: true, message: '0-未出租 1-出租中 2-合租未满不能为空', trigger: 'blur' }
          ],
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          xianId: [
            { required: true, message: '地区id(县id)不能为空', trigger: 'blur' }
          ],
          rentalAddress: [
            { required: true, message: '出租房地址不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          dimension: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          rentalDirection: [
            { required: true, message: '出租房朝向不能为空', trigger: 'blur' }
          ],
          cellId: [
            { required: true, message: '小区id不能为空', trigger: 'blur' }
          ],
          cellName: [
            { required: true, message: '小区名称不能为空', trigger: 'blur' }
          ],
          unitName: [
            { required: true, message: '户型不能为空', trigger: 'blur' }
          ],
          rentalImage: [
            { required: true, message: '出租房图集不能为空', trigger: 'blur' }
          ],
          decolate: [
            { required: true, message: '装修情况（1 毛坯 2简装 3精装 4豪装）不能为空', trigger: 'blur' }
          ],
          rentalFloor: [
            { required: true, message: '出租房楼层不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.updForm.rentalId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['updForm'].resetFields()
          if (this.updForm.rentalId) {
            this.$http({
              url: this.$http.adornUrl(`/house/rental/info/${this.updForm.rentalId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.updForm.rentalName = data.rental.rentalName
                this.updForm.rentalTitle = data.rental.rentalTitle
                this.updForm.rentalArea = data.rental.rentalArea
                this.updForm.rentalPrice = data.rental.rentalPrice
                this.updForm.rentalStatus = data.rental.rentalStatus
                this.updForm.shenId = data.rental.shenId
                this.updForm.shiId = data.rental.shiId
                this.updForm.xianId = data.rental.xianId
                this.updForm.rentalAddress = data.rental.rentalAddress
                this.updForm.longitude = data.rental.longitude
                this.updForm.dimension = data.rental.dimension
                this.updForm.rentalDirection = data.rental.rentalDirection
                this.updForm.cellId = data.rental.cellId
                this.updForm.cellName = data.rental.cellName
                this.updForm.unitName = data.rental.unitName
                this.updForm.rentalImage = data.rental.rentalImage
                this.updForm.decolate = data.rental.decolate
                this.updForm.rentalFloor = data.rental.rentalFloor
                this.updForm.brokerId = data.rental.brokerId
              }
            })
          }
        })
      },
      // 表单提交
      updFormSubmit () {
        this.$refs['updForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/rental/${!this.updForm.rentalId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentalId': this.updForm.rentalId || undefined,
                'rentalName': this.updForm.rentalName,
                'rentalTitle': this.updForm.rentalTitle,
                'rentalArea': this.updForm.rentalArea,
                'rentalPrice': this.updForm.rentalPrice,
                'rentalStatus': this.updForm.rentalStatus,
                'shenId': this.updForm.shenId,
                'shiId': this.updForm.shiId,
                'xianId': this.updForm.xianId,
                'rentalAddress': this.updForm.rentalAddress,
                'longitude': this.updForm.longitude,
                'dimension': this.updForm.dimension,
                'rentalDirection': this.updForm.rentalDirection,
                'cellId': this.updForm.cellId,
                'cellName': this.updForm.cellName,
                'unitName': this.updForm.unitName,
                'rentalImage': this.updForm.rentalImage,
                'decolate': this.updForm.decolate,
                'rentalFloor': this.updForm.rentalFloor,
                'brokerId': this.updForm.brokerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
